package courses;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

class MyPanel extends JPanel { // 定义我的画板
    @Override
    public void paint(Graphics g) {
        Image image = new ImageIcon("星空.jpg").getImage();
        g.drawImage(image, 0, 0, null);
    }
}

public class MeteorDemo {
    JFrame jf;
    MyPanel mp;
    class MeteorThread extends Thread{
        int x,y;// 圆的起点坐标
        Graphics g;
        public MeteorThread(int x, int y,Graphics g) {
            this.x = x;
            this.y = y;
            this.g=g;
        }

        @Override
        public void run() {
            int i=5,dx=5,dy=-4,r=12;
            g.setColor(Color.white);
            g.fillOval(x,y,r,r);
            while (true) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                g.fillOval(x+i*dx,y+i*dy,r,r);
                i++;
                if (i % 5 == 0) {
                    i--;
                }
            }
        }
    }
    public void init() {
        jf = new JFrame("流星案例");
        mp=new MyPanel();

        // 对面板添加鼠标监听器
        mp.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new MeteorThread(e.getX(),e.getY(),mp.getGraphics()).start();
            }
        });

        jf.add(mp);
        jf.setVisible(true);
        jf.setSize(600, 600);
    }

    public static void main(String[] args) {
        new MeteorDemo().init();
    }
}
